<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Parents extends CI_Controller {

	public function __construct(){
		parent::__construct();
		$this->load->model('login_model','l_db');
		$this->load->model('parents_model','get_db');
	}

	/**
	 * Index Page for this controller.
	 */
	public function index()
	{
		if($this->session->userdata('username')){
			$header['menu'] = $this->l_db->generate_menu();
			$data['grid'] = $this->get_db->get_list()->result_array();
			$footer['js'][] = "<script type='text/javascript'>var base_url = '".base_url().index_page()."';</script>";
			$footer['js'][] = '<script src="'.app_asset_url().'js/app/grid_parents.js"></script>';
			$this->load->view('header_view',$header);
			$this->load->view('parents_view',$data);
			$this->load->view('footer_view',$footer);
		}else{
			$this->session->set_flashdata( 'message', array( 'title' => 'Error', 'content' => 'Please login !', 'type' => 'message' )); 
			redirect();
		}
	}

	public function add_parents()
	{
		if($this->session->userdata('username')){
			$header['menu'] = $this->l_db->generate_menu();
			$data['grid'] = $this->get_db->get_list()->result_array();
			$this->load->view('header_view',$header);
			$this->load->view('add_parents_view',$data);
			$this->load->view('footer_view');
		}else{
			$this->session->set_flashdata( 'message', array( 'title' => 'Error', 'content' => 'Please login !', 'type' => 'message' )); 
			redirect();
		}
	}

	public function edit_parents($id = false)
	{
		if($this->session->userdata('username')){
			$header['menu'] = $this->l_db->generate_menu();
			$data['data'] = $this->get_db->get_data($id)->result_array();
			$this->load->view('header_view',$header);
			$this->load->view('edit_parents_view',$data);
			$this->load->view('footer_view');
		}else{
			$this->session->set_flashdata( 'message', array( 'title' => 'Error', 'content' => 'Please login !', 'type' => 'message' )); 
			redirect();
		}
	}		

	public function save($id = false){
		if($_POST){			
			// Update
			if($id){		
				if($this->get_db->do_update($id,$_POST)) $this->session->set_flashdata( 'message', array( 'title' => 'Info', 'content' => 'Berhasil merubah !', 'type' => 'alert alert-success' )); 
				else $this->session->set_flashdata( 'message', array( 'title' => 'Error', 'content' => 'Gagal merubah !', 'type' => 'alert alert-errore' )); 
				redirect('parents');
			}else{
				if($this->get_db->do_add($_POST)) $this->session->set_flashdata( 'message', array( 'title' => 'Info', 'content' => 'Berhasil menambah !', 'type' => 'alert alert-success' )); 
				else $this->session->set_flashdata( 'message', array( 'title' => 'Error', 'content' => 'Gagal menambah !', 'type' => 'alert alert-errore' )); 
				redirect('parents');			
			}
		}else{
			die('No direct access!');
		}
	}

	public function delete($id = false){
		if($id){
			$ids = explode('_',$id);
			$tid = count($ids);
			if($tid == 1){
				if($this->get_db->do_delete($id))  $this->session->set_flashdata( 'message', array( 'title' => 'Info', 'content' => 'Berhasil menghapus !', 'type' => 'alert alert-success' )); 
				else $this->session->set_flashdata( 'message', array( 'title' => 'Error', 'content' => 'Gagal menghapus !', 'type' => 'alert alert-errore' )); 
			}else{
				$success = 0;
				foreach($ids as $i){
					if($this->get_db->do_delete($i)) $success++;
				}
				if($success > 0)  $this->session->set_flashdata( 'message', array( 'title' => 'Info', 'content' => $success.' dari '.$tid.' berhasil dihapus !', 'type' => 'alert alert-success' )); 
				else  $this->session->set_flashdata( 'message', array( 'title' => 'Error', 'content' => 'Gagal menghapus !', 'type' => 'alert alert-errore' )); 
			}
			redirect('parents');
		}else{
			die('No direct access!');		
		}
	}
}
